package com.fr.report.parameter;

import com.fr.base.DefaultValues;
import com.fr.base.FRContext;
import com.fr.base.Nameable;
import com.fr.base.XMLable;
import com.fr.base.core.BaseCoreUtils;
import com.fr.base.core.ComparatorUtils;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLableReader;
import com.fr.report.cellElement.Formula;
import com.fr.report.io.xml.ReportXMLUtils;
import com.fr.report.io.xml.SynchronizedNameWidget;
import com.fr.report.io.xml.SynchronizedVersion;
import com.fr.report.script.Calculator;
import com.fr.report.web.ui.FieldEditor;
import com.fr.util.Utils;
import java.io.Serializable;
import java.text.DateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Parameter
  implements XMLable, Nameable, Serializable
{
  public static final String XML_TAG = "Parameter";
  public static final String ARRAY_XML_TAG = "Parameters";
  private String name;
  private Object value;

  public Parameter()
  {
    this("");
  }

  public Parameter(String paramString)
  {
    this(paramString, "");
  }

  public Parameter(String paramString, Object paramObject)
  {
    this.name = null;
    this.value = null;
    setName(paramString);
    setValue(paramObject);
  }

  public String getName()
  {
    return this.name;
  }

  public void setName(String paramString)
  {
    this.name = paramString;
  }

  public Object getValue()
  {
    return this.value;
  }

  public void setValue(Object paramObject)
  {
    this.value = paramObject;
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    if (paramXMLableReader.isChildNode())
    {
      Object localObject;
      String str2 = paramXMLableReader.getTagName();
      if (("Object".equals(str2)) || ("O".equals(str2)))
      {
        localObject = ReportXMLUtils.readObject(paramXMLableReader);
        if ((localObject instanceof String) && (((String)localObject).startsWith("=")) && (SynchronizedVersion.getSynchronizedVersion(Thread.currentThread()).getVersion() < 6.5D))
          localObject = new Formula((String)localObject);
        setValue(localObject);
      }
      else if ("Attributes".equals(str2))
      {
        String str1;
        if ((str1 = paramXMLableReader.getAttr("name")) != null)
          setName(str1);
      }
      else if (("Widget".equals(str2)) || (str2.equals("CellEditorDef")))
      {
        localObject = ReportXMLUtils.readFieldEditor(paramXMLableReader);
        SynchronizedNameWidget.put(getName(), (FieldEditor)localObject);
        SynchronizedNameWidget.put(getName(), this.value);
      }
    }
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    paramXMLPrintWriter.startTAG("Attributes").attr("name", getName()).end();
    if (getValue() != null)
      ReportXMLUtils.writeObject(paramXMLPrintWriter, getValue());
  }

  public Object clone()
    throws CloneNotSupportedException
  {
    Parameter localParameter = (Parameter)super.clone();
    if (this.value != null)
      localParameter.value = BaseCoreUtils.cloneObject(this.value);
    return localParameter;
  }

  public String toString()
  {
    return "$" + getName();
  }

  public String valueToString()
  {
    String str = "";
    if (this.value != null)
    {
      Object localObject;
      if (this.value instanceof Formula)
      {
        localObject = ((Formula)this.value).getResult();
        if (localObject == null)
          try
          {
            localObject = Calculator.createCalculator().eval(((Formula)this.value).getContent().substring(1));
          }
          catch (Exception localException)
          {
            FRContext.getLogger().log(Level.WARNING, localException.getMessage(), localException);
          }
        if (localObject != null)
          return localObject.toString();
        return "";
      }
      if (this.value instanceof Date)
      {
        localObject = FRContext.getDefaultValues();
        str = ((DefaultValues)localObject).getDateTimeFormat().format(this.value);
      }
      else if (this.value instanceof Double)
      {
        str = Utils.doubleToString(((Double)this.value).doubleValue());
      }
      else
      {
        str = this.value.toString();
      }
    }
    return ((String)str);
  }

  public boolean equals(Object paramObject)
  {
    return ((paramObject instanceof Parameter) && (ComparatorUtils.equals(this.name, ((Parameter)paramObject).name)) && (ComparatorUtils.equals(this.value, ((Parameter)paramObject).value)));
  }
}